package cn.com.fssg.platform.base.handler.impl.pjqc;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 评价期次设置标段是否参评
 */
@Component
public class BdSetIsCp implements InterfaceHandler {

//    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        // 标段id
        String ids = jsonObject.get("ids").toString();
        // 评价期次id
        String qcid = jsonObject.get("qcid").toString();
        List<String> idList = Arrays.asList(ids.split(","));
        // 是否参评
        boolean iscp = jsonObject.getBoolean("iscp");
        String sql = "";
        // 是否参评都是先删除原先记录
        HashMap<String, Object> params = new HashMap<>();
        params.put("ids", idList);
        params.put("qcid", qcid);
        // 设置参评也就需要移除不参评设置记录
        sql = "DELETE FROM bs_pjqc_bcpbd WHERE parentid=:qcid and bdid in (:ids)";
        jdbcTemplate.update(sql, params);
        if (!iscp) {
            // 不参评生成记录
            String insertSql = "INSERT INTO bs_pjqc_bcpbd (id, parentid, bdid) VALUES (:id, :qcid,:bdid)";
            List<Map<String, Object>> batchParams = new ArrayList<>();
            for (String bdid : idList) {
                params = new HashMap<>();
                params.put("bdid", bdid);
                params.put("qcid", qcid);
                // 生成记录
                params.put("id", BizUtil.cur().getSnowflakeIdStr());
                batchParams.add(params);
            }
            if (!CollectionUtils.isEmpty(batchParams)) {
                jdbcTemplate.batchUpdate(insertSql, batchParams.toArray(new Map[0]));
            }
        }
        return null;
    }

}
